package com.zhoug.zmuidemo.ui;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.zhoug.zmui.dialog.menu.Menu;
import com.zhoug.zmui.dialog.menu.ZMUIMenuDialog;
import com.zhoug.zmui.round.ZMUIRoundButton;
import com.zhoug.zmuidemo.MyMenu;
import com.zhoug.zmuidemo.R;
import com.zhoug.zmuidemo.treeview.AndroidTreeView;
import com.zhoug.zmuidemo.treeview.TreeNode;

import java.util.ArrayList;
import java.util.List;

public class MenuActivity extends AppCompatActivity {
    private ZMUIRoundButton mZMUIRoundButton;
    private List<MyMenu> data = new ArrayList<>();
    private ZMUIRoundButton mBtn2;
    private FrameLayout mFrameLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_menu);
        findViews();
      for(int i=0;i<5;i++){
          MyMenu myMenu = new MyMenu("code"+i, "menu"+i);
          initData(1,myMenu);
          data.add(myMenu);
      }

        mZMUIRoundButton.setOnClickListener(v -> {
            ZMUIMenuDialog menuDialog = new ZMUIMenuDialog(this);
            menuDialog.setMenus(data);
            menuDialog.setSelectColor(Color.RED);
            menuDialog.setCallback(new ZMUIMenuDialog.Callback() {
                @Override
                public void onSelected(Menu menu) {
                    Toast.makeText(MenuActivity.this, menu.getName(), Toast.LENGTH_SHORT).show();
                }
            });

            menuDialog.show();
        });

      mBtn2.setOnClickListener(v->{

      });

      init();
    }


    private void init(){
        TreeNode root= TreeNode.root();
        TreeNode parent = new TreeNode("MyParentNode");
        TreeNode child0 = new TreeNode("ChildNode0");
        TreeNode child1 = new TreeNode("ChildNode1");
        TreeNode child2 = new TreeNode("ChildNode2");

        TreeNode child01 = new TreeNode("ChildNode01");
        TreeNode child02 = new TreeNode("ChildNode02");
        TreeNode child03 = new TreeNode("ChildNode03");

        TreeNode child011 = new TreeNode("child011");
        TreeNode child012 = new TreeNode("child012");
        TreeNode child013 = new TreeNode("child013");


        child01.addChildren(child011, child012,child013);

        child0.addChildren(child01, child02,child03);

        parent.addChildren(child0, child1,child2);
        root.addChild(parent);
        AndroidTreeView tView = new AndroidTreeView(this, root);
        mFrameLayout.addView(tView.getView());



    }

    public class MyHolder extends TreeNode.BaseNodeViewHolder<IconTreeItem> {


        public MyHolder(Context context) {
            super(context);
        }

        @Override
        public View createNodeView(TreeNode node, IconTreeItem value) {
            View root = LayoutInflater.from(MenuActivity.this).inflate(R.layout.layout_treeview, null);
            ImageView imageView=root.findViewById(R.id.icon);
            TextView textView=root.findViewById(R.id.text);
            imageView.setImageResource(value.icon);
            textView.setText(value.text);

            return root;
        }
    }

    public static class IconTreeItem {
        public int icon;
        public String text;
    }


    private void initData(int count,MyMenu menu) {
       if(count<=5){
           List<MyMenu> child=new ArrayList<>();
           for(int i=0;i<10;i++){
               MyMenu myMenu = new MyMenu(menu.getCode() + "_" + i, menu.getName() + "_" + i);
               initData(++count,myMenu);
               child.add(myMenu);
           }
           menu.setChild(child);
       }
    }

    private void findViews() {
        mZMUIRoundButton = findViewById(R.id.zMUIRoundButton);
        mBtn2 = findViewById(R.id.btn_2);
        mFrameLayout = findViewById(R.id.frameLayout);
    }


}